package middleware

import (
	"github.com/cy77cc/hioshop/global"
	"github.com/gin-gonic/gin"
	"github.com/joho/godotenv"
	"net/http"
	"os"
)

func LoadEnv() gin.HandlerFunc {
	return func(c *gin.Context) {
		err2 := godotenv.Load()
		if err2 != nil {
			c.JSON(http.StatusServiceUnavailable, gin.H{
				"message": "Error loading .env file",
			})
			c.Abort()
			return
		}
		// 设置微信小程序的appid和密钥
		c.Set("appid", os.Getenv("appid"))
		c.Set("secret", os.Getenv("secret"))
		//c.Set("access-key", os.Getenv("QINIU_ACCESS_KEY"))
		//c.Set("secret-key", os.Getenv("QINIU_SECRET_KEY"))
		global.CONFIG.Qiniu.AccessKey = os.Getenv("QINIU_ACCESS_KEY")
		global.CONFIG.Qiniu.SecretKey = os.Getenv("QINIU_SECRET_KEY")
		c.Next()
	}
}
